/*
 * @Author: Black / 李来然
 * @Date: 2021-12-20 16:54:26
 * @LastEditors: Black / 李来然
 * @LastEditTime: 2022-01-26 13:44:00
 * @Description: 摄像头模块流程
 */
import { Cam } from "@/ecjrts/mode";
import { GetSomeTypeKey } from "@/ecjrts/tools/modHelper";
import { useModule, ModsInstance } from "@/func/createMod";

export type CamMod = GetSomeTypeKey<Required<ModsInstance>, Cam>;

export interface WindowOpt {
	width: number;
	height: number;
	x: number;
	y: number;
} 

export interface CamFnOpt<T> {
	mod?: T;
}


export const StartDisplay = async function (param: CamFnOpt<CamMod>, e: WindowOpt) {
	// CameraID:string,
	// Trace(EC_INFO, `->StartDisplay CameraID=${CameraID}`);
	const io = useModule();
	let { mod: modName = "cam" } = param;
	let ctrl = io[modName];
	const { glt } = io;

	if (!ctrl) {
		throw new Error(`没有${modName}模块`);
	}
	let state = await ctrl.GetState();
	console.log(state);
	if (state.Result === "OK") {
		let strPosition = "PERSON";
		glt?.SetGuidLigtEx("CAMERA", "MEDIUM");
		Trace(EC_INFO, `--CameraID = ${strPosition}`);

		let retStartDisply = await ctrl.StartDisplay(strPosition, e);
		// console.log(retStartDisply);
		if (retStartDisply) {
			Trace(EC_INFO, "<-StartDisplay ret=" + retStartDisply.ret);
			return retStartDisply.ret;
		} else {
			glt?.SetGuidLigtEx("CAMERA", "OFF");
			Trace(EC_INFO, "<-StartDisplay ret=FatalError");
			return "FatalError";
		}
	} else {
		return "FatalError";
	}
};

/**
 * 关闭预览框
 * @param {object} param {position:{}}
 */
export const StopDisplay = async function (param: CamFnOpt<CamMod>, isCancel = false) {
	Trace(EC_INFO, "-> StopDisplay");
	const io = useModule();
	let { mod: modName = "cam" } = param;
	let ctrl = io[modName];
	const { glt } = io;

	if (!ctrl) {
		throw new Error(`没有${modName}模块`);
	}

	// let obj = param;
	// let position = obj['position'];

	let strPosition = "PERSON";

	// 活体检测摄像头必须先取消命令，才能关闭
	if(isCancel) {
		let retCancel =  await ctrl.CancelAllRequest();
		console.log(`取消指令结果${retCancel}`);
	}
	
	glt?.SetGuidLigtEx("CAMERA", "OFF");

	let ret;
	if(isCancel) {
		ret = await ctrl.StopDisplay(strPosition, true);
	} else {
		ret = await ctrl.StopDisplay(strPosition);
	}
	if (ret) {
		Trace(EC_INFO, "<-StopDisplay ret=" + ret.ret);
		return ret.ret;
	} else {
		Trace(EC_INFO, "<-StopDisplay ret=FatalError");
		return "FatalError";
	}
};

/**
 * 拍照
 * @param {} parm
 */
export const TakePicture = async function (
	param: CamFnOpt<CamMod>,
	strPathNum: string,
	isExInterface = false
) {
	Trace(EC_INFO, "-> TakePicture");
	const io = useModule();
	let { mod: modName = "cam" } = param;
	let ctrl = io[modName];
	const { common } = io;

	if (!ctrl) {
		throw new Error(`没有${modName}模块`);
	}
	if (!common) {
		throw new Error(`没有common模块`);
	}
	// if (parm.length <= 0) return 'error';
	// let obj = parm;
	console.log(strPathNum);

	let scanAction = `CustomerImg${strPathNum}`;
	console.log(scanAction);

	let strPath = ``;
	let isPathFileExist = await common.IsPathFileExists('c:\\camera');
	console.log(isPathFileExist);
	if(isPathFileExist !== 0) {
		await common.CreateDir('c:\\camera')
	}
	if(modName === 'face') {
		common.DeleteFile(`c:\\camera\\${scanAction}-Ir.jpg`);
		common.DeleteFile(`c:\\camera\\${scanAction}-Vl.jpg`);
		strPath = `c:\\camera\\${scanAction}-Vl.jpg;c:\\camera\\${scanAction}-Ir.jpg`;
	} else {
		common.DeleteFile(`c:\\camera\\${scanAction}.jpg`);
		strPath = `;c:\\camera\\${scanAction}.jpg`;
	}
	
	// let position = obj['position'];

	let strPosition = "PERSON";
	// let bmpName = scanAction + '.jpg';
	let ret;
	if(isExInterface) {
		ret = await ctrl.TakePictureEX(strPosition, '', strPath);
	} else {
		ret = await ctrl.TakePicture(strPosition, strPath);
	}
	console.log(strPath);

	if (ret.ret) {
		// await cam?.CancelAllRequest();
		// let ret2 = await cam.StopDisplay(strPosition);
		// console.log(ret2);

		switch (ret.ret) {
			case "TakePictureSuccess":
			case "TakePictureEXSuccess":
				Trace(EC_INFO, "<- TakePicture ret=" + ret.ret);
				return ret.ret;
			default:
				Trace(EC_INFO, "<- TakePicture ret=" + ret.ret);
				return ret.ret;
		}
	} else {
		Trace(EC_INFO, "<- TakePicture ret=FatalError");
		return "FatalError";
	}
};

/**
 * 拍照2
 * @param {} parm
 */
export const TakePicture2 = async function (
) {
	Trace(EC_INFO, "-> TakePicture");
	const io = useModule();
	const { glt, cam, common } = io;
	if (!cam) {
		throw new Error(`没有cam模块`);
	}
	if (!common) {
		throw new Error(`没有common模块`);
	}
	// if (parm.length <= 0) return 'error';
	// let obj = parm;
	// console.log(strPathNum);

	let scanAction = 'CompareFace';
	console.log(scanAction);

	common?.DeleteFile(`c:\\${scanAction}.jpg`);
	// let position = obj['position'];

	let strPosition = "PERSON";
	let strPath = `;c:\\${scanAction}.jpg`;
	// let bmpName = scanAction + '.jpg';

	let ret = await cam?.TakePicture(strPosition, strPath);
	console.log(strPath);

	if (ret?.ret) {
		// await cam?.CancelAllRequest();
		let ret2 = await cam?.StopDisplay(strPosition);
		console.log(ret2);

		switch (ret.ret) {
			case "TakePictureSuccess":
				// 拍照成功
				// 读取base64让上层自己写
				/* let data = await com.ReadImageBase64(strPath);
		let res = { pic: data, path: strPath, name: bmpName };
		Trace(EC_INFO, '<- TakePicture ret=' + JSON.stringify({ret: ret.ret, res: res}));
		return {ret: ret.ret, res: res}; */
				Trace(EC_INFO, "<- TakePicture ret=" + ret.ret);
				return ret.ret;
			default:
				Trace(EC_INFO, "<- TakePicture ret=" + ret.ret);
				return ret.ret;
		}
	} else {
		Trace(EC_INFO, "<- TakePicture ret=FatalError");
		return "FatalError";
	}
};

/**
 * 重置摄像头
 */
export const CamReset = async function () {
	Trace(EC_INFO, "->CamReset");
	const io = useModule();
	const { glt, cam, common } = io;
	if (!cam) {
		throw new Error(`没有cam模块`);
	}
	let ret = await cam?.Reset();
	if (ret) {
		Trace(EC_INFO, "<-CamReset ret=" + ret.ret);
		return ret.ret;
	} else {
		Trace(EC_INFO, "<-CamReset ret=FatalError");
		return "FatalError";
	}
};

/**
 * 取消指令
 */
export const CancelAllRequest = async function () {
	Trace(EC_INFO, "->CancelAllRequest");
	const io = useModule();
	const { glt, cam, common } = io;
	if (!cam) {
		throw new Error(`没有cam模块`);
	}
	let ret = await cam?.CancelAllRequest();
	console.log(ret);
};
